﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Template.Template
{
    public partial class TemplateDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int uid = 0;
                CmsTemplateInfo temp = null;
                if (Int32.TryParse(Request["uid"], out uid))
                {
                    using(ISession session = SessionFactoryHelper.CreateSession())
                    {
                        temp = session.Get<CmsTemplateInfo>(uid);
                    }
                    if (temp != null)
                    {
                        name.Text = temp.Name;
                        memo.Text = temp.Memo;

                        fc_imgurl.Value = temp.ImageUrl;
                        sort.Text = temp.Sort.ToString();
                        ViewState["uid"] = uid;
                    }
                }
            }
            else
            {
                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];

                
                CmsTemplateInfo temp = null;
                using(ISession session = SessionFactoryHelper.CreateSession())
                {
                    temp = session.Get<CmsTemplateInfo>(uid);
                    if (temp == null)
                    {
                        temp = new CmsTemplateInfo();
                        temp.CreatDate = DateTime.Now;
                    }
                    temp.Name = name.Text;
                    temp.Memo = memo.Text;
                    try
                    {
                        temp.Sort = Int32.Parse(sort.Text);
                    }
                    catch (System.Exception ex)
                    {
                	
                    }
                    temp.ImageUrl = fc_imgurl.Value;
                    session.SaveOrUpdate(temp);
                    session.Flush();
                }

                FrameHelper _framePage = new FrameHelper(this);
                ReturnCode rc = new ReturnCode()
                {
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Template/Template/TemplateDetail.aspx?tabid=TemplateDetail";

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + temp.Id;
                }

                _framePage.Alert(rc);

            }

        
        }
    }
}